package org.mathcloud.math.core.generic.operator.algebra;

import org.mathcloud.math.core.common.operator.algebra.Minus;
import org.mathcloud.math.core.common.quantity.ComplexNumber;
import org.mathcloud.math.core.common.quantity.QuantityObject;
import org.mathcloud.math.core.common.quantity.RealNumber;
import org.mathcloud.math.core.common.signature.QuantityObjectSignature;
import org.mathcloud.math.core.generic.operator.GenericBinaryOperator;
import org.mathcloud.math.core.generic.quantity.GenericComplexNumber;
import org.mathcloud.math.core.generic.quantity.GenericRealNumber;


public class GenericMinus extends GenericBinaryOperator implements Minus
{
	public GenericMinus()
	{
		super();
		signature.setMMLContentTag("minus");
	}
	
	public void setInputParams(QuantityObject in)
	{
		operand1 = new GenericRealNumber();
		operand2 = in;
	}
	
	@Override
	public void calculate()
	{
		QuantityObjectSignature resSign = getResultSignature(); 
		if (resSign!= null)
		{
			if (resSign.isRealNumberSignature())
			{
				res = new GenericRealNumber(((RealNumber)operand1).getValue()-((RealNumber)operand2).getValue());
			}
			else if (resSign.isComplexNumberSignature())
			{
	      double real, image;
	      real = image = 0;
	      if (operand1.getSignature().isRealNumberSignature())
	      {
	        real = ((RealNumber)operand1).getValue();
	      } else if (operand1.getSignature().isComplexNumberSignature())
	      {
	        real = ((ComplexNumber)operand1).getRealPart();
	        image = ((ComplexNumber)operand1).getImagePart();
	      }
	      if (operand2.getSignature().isRealNumberSignature())
	      {
	        real = real - ((RealNumber)operand2).getValue();
	      } else if (operand2.getSignature().isComplexNumberSignature())
	      {
	        real = real - ((ComplexNumber)operand2).getRealPart();
	        image = image - ((ComplexNumber)operand2).getImagePart();
	      }
	      res = new GenericComplexNumber(real,image);
			}
		}
	}
}
